package com.hyk.commonLib.common.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.widget.ImageView;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseBinderAdapter;
import com.chad.library.adapter.base.binder.QuickItemBinder;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hyk.commonLib.R;
import com.hyk.commonLib.common.listener.OnClickResultListener;
import com.hyk.commonLib.common.utils.ResUtils;
import com.hyk.commonLib.common.utils.ScreenUtils;
import com.hyk.commonLib.databinding.CommonLibViewTopBarGeneratorBinding;

import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.Collection;

/**
 * Created by 贺玉琨 on 2019/3/14.
 */
public class TopBarGenerator extends ConstraintLayout {

    private OnClickResultListener onBackClickListener;
    private OnClickResultListener onCloseClickListener;
    private OnClickResultListener onFunctionClickResultListener;

    protected BaseBinderAdapter baseBinderAdapter;

    protected CommonLibViewTopBarGeneratorBinding dataBinding;

    public TopBarGenerator(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        if (!isInEditMode()) {
            dataBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.common_lib_view_top_bar_generator, this, true);
            initAttr(attrs);
        }
    }

    private void initAttr(@Nullable AttributeSet attrs) {
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.TopBarGenerator);
        @ColorInt int backColor = a.getColor(R.styleable.TopBarGenerator_backColor, Color.TRANSPARENT);
        @ColorInt int secondaryBackColor = a.getColor(R.styleable.TopBarGenerator_secondaryBackColor, backColor);
        @ColorInt int titleColor = a.getColor(R.styleable.TopBarGenerator_titleColor, Color.BLACK);
        @ColorInt int backBtnColor = a.getColor(R.styleable.TopBarGenerator_backBtnColor, Color.BLACK);
        @ColorInt int closeBtnColor = a.getColor(R.styleable.TopBarGenerator_closeBtnColor, Color.BLACK);
        @ColorInt int functionBtnColor = a.getColor(R.styleable.TopBarGenerator_functionBtnColor, Color.BLACK);
        float titleSize = a.getDimension(R.styleable.TopBarGenerator_titleSize, ScreenUtils.dp2px(15, this));
        boolean showBack = a.getBoolean(R.styleable.TopBarGenerator_showBack, true);
        boolean showClose = a.getBoolean(R.styleable.TopBarGenerator_showClose, false);
        boolean showFunction = a.getBoolean(R.styleable.TopBarGenerator_showFunction, false);
        boolean showSubFunction = a.getBoolean(R.styleable.TopBarGenerator_showSubFunction, true);
        String title = a.getString(R.styleable.TopBarGenerator_title);
        a.recycle();

        setBackVisible(showBack);
        setCloseVisible(showClose);
        setFunctionVisible(showFunction);
        setTitle(title);
        setTitleColor(titleColor);
        setBackBtnColor(backBtnColor);
        setCloseBtnColor(closeBtnColor);
        setFunctionBtnColor(functionBtnColor);
        setBackColor(backColor, secondaryBackColor);
        setTitleSize(titleSize);
        initSubFunction();
        setShowSubFunction(showSubFunction);

        dataBinding.setOnBackClickListener(v -> onBackClicked());
        dataBinding.setOnCloseClickListener(v -> onCloseClicked());
        dataBinding.setOnFunctionClickResultListener(v -> onFunctionClicked());
    }

    private void initSubFunction() {
        LinearLayoutManager manager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
        dataBinding.rcySubFunction.setLayoutManager(manager);
        baseBinderAdapter = new BaseBinderAdapter()
                .addItemBinder(SubFunctionBtn.class, new SubFunctionBinder());
        dataBinding.rcySubFunction.setAdapter(baseBinderAdapter);
    }

    public void setTitle(String title) {
        dataBinding.txtTitle.setText(title);
    }

    public CharSequence getTitle() {
        return dataBinding.txtTitle.getText();
    }

    public void setBackVisible(boolean visible) {
        dataBinding.imgBack.setVisibility(visible ? VISIBLE : GONE);
    }

    public void setCloseVisible(boolean visible) {
        dataBinding.imgClose.setVisibility(visible ? VISIBLE : GONE);
    }

    public void setFunctionVisible(boolean visible) {
        dataBinding.imgFunction.setVisibility(visible ? VISIBLE : GONE);
    }

    public void setTitleColor(@ColorInt int titleColor) {
        dataBinding.txtTitle.setTextColor(titleColor);
    }

    public void setTitleSize(float titleSize) {
        dataBinding.txtTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
    }

    public void setBackBtnColor(@ColorInt int color) {
        setImgColorFilter(dataBinding.imgBack, color);
    }

    public void setCloseBtnColor(@ColorInt int color) {
        setImgColorFilter(dataBinding.imgClose, color);
    }

    public void setFunctionBtnColor(@ColorInt int color) {
        setImgColorFilter(dataBinding.imgFunction, color);
    }

    public void setSubFunctionBtn(Collection<SubFunctionBtn> subFunctionBtnList) {
        baseBinderAdapter.setList(subFunctionBtnList);
    }

    public void setSubFunctionBtn(SubFunctionBtn... subFunctionBtnList) {
        setSubFunctionBtn(Arrays.asList(subFunctionBtnList));
    }

    public void setShowSubFunction(boolean showSubFunction) {
        dataBinding.rcySubFunction.setVisibility(showSubFunction ? VISIBLE : GONE);
    }

    public void setBackColor(@ColorInt int backColor, @ColorInt int secondaryBackColor) {
        GradientDrawable drawable = new GradientDrawable();
        drawable.setColors(new int[]{backColor, secondaryBackColor});
        drawable.setOrientation(GradientDrawable.Orientation.TOP_BOTTOM);
        drawable.setGradientRadius(0);
        setBackground(drawable);
    }

    private void setImgColorFilter(ImageView img, @ColorInt int color) {
        img.clearColorFilter();
        img.setColorFilter(color);
    }

    public void onBackClicked() {
        if (onBackClickListener == null || !onBackClickListener.onClick()) {
            //当未设置onBackClickListener或返回false时，调用默认处理事件
            Activity activity = getActivity();
            if (activity != null) {
                activity.finish();
            }
        }
    }

    public void onCloseClicked() {
        if (onCloseClickListener == null || !onCloseClickListener.onClick()) {
            //当未设置onBackClickListener或返回false时，调用默认处理事件
            Activity activity = getActivity();
            if (activity != null) {
                activity.finish();
            }
        }
    }

    public void onFunctionClicked() {
        if (onFunctionClickResultListener != null) {
            onFunctionClickResultListener.onClick();
        }
    }

    public void setOnBackClickListener(OnClickResultListener onBackClickListener) {
        this.onBackClickListener = onBackClickListener;
    }

    public void setOnCloseClickListener(OnClickResultListener onCloseClickListener) {
        this.onCloseClickListener = onCloseClickListener;
    }

    public void setOnFunctionClickListener(OnClickResultListener onFunctionClickResultListener) {
        this.onFunctionClickResultListener = onFunctionClickResultListener;
    }

    @Nullable
    private Activity getActivity() {
        if (getContext() instanceof Activity) {
            return (Activity) getContext();
        } else if (getContext() instanceof ContextThemeWrapper && ((ContextThemeWrapper) getContext()).getBaseContext() instanceof Activity) {
            return (Activity) ((ContextThemeWrapper) getContext()).getBaseContext();
        }
        return null;
    }

    public static class SubFunctionBtn {
        private final Drawable drawable;
        private final OnClickResultListener onClickResultListener;
        @ColorInt
        private final int color;

        public SubFunctionBtn(Drawable drawable) {
            this(drawable, null);
        }

        public SubFunctionBtn(Drawable drawable, OnClickResultListener onClickResultListener) {
            this(drawable, onClickResultListener, Color.TRANSPARENT);
        }

        public SubFunctionBtn(Drawable drawable, int color) {
            this(drawable, null, color);
        }

        public SubFunctionBtn(@DrawableRes int drawableRes) {
            this(ResUtils.getDrawable(drawableRes));
        }

        public SubFunctionBtn(@DrawableRes int drawableRes, OnClickResultListener onClickResultListener) {
            this(ResUtils.getDrawable(drawableRes), onClickResultListener);
        }

        public SubFunctionBtn(@DrawableRes int drawableRes, int color) {
            this(ResUtils.getDrawable(drawableRes), color);
        }

        public SubFunctionBtn(@DrawableRes int drawableRes, OnClickResultListener onClickResultListener, int color) {
            this(ResUtils.getDrawable(drawableRes), onClickResultListener, color);
        }

        public SubFunctionBtn(Drawable drawable, OnClickResultListener onClickResultListener, int color) {
            this.drawable = drawable;
            this.onClickResultListener = onClickResultListener;
            this.color = color;
        }
    }

    private static class SubFunctionBinder extends QuickItemBinder<SubFunctionBtn> {
        @Override
        public int getLayoutId() {
            return R.layout.common_lib_view_top_bar_sub_btn;
        }

        @Override
        public void convert(@NotNull BaseViewHolder baseViewHolder, SubFunctionBtn btn) {
            ImageView img = baseViewHolder.getView(R.id.imgFunction);
            img.setImageDrawable(btn.drawable);
            if (btn.color != Color.TRANSPARENT) {
                img.setColorFilter(btn.color);
            } else {
                img.clearColorFilter();
            }
            img.setOnClickListener(v -> {
                if (btn.onClickResultListener != null) btn.onClickResultListener.onClick();
            });
        }
    }
}
